{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 变量\n",
    "\n",
    "Python 中的变量不需要声明。每个变量在使用前都必须赋值，变量赋值以后该变量才会被创建。\n",
    "\n",
    "在 Python 中，变量就是变量，它没有类型，我们所说的\"类型\"是变量所指的内存中对象的类型。\n",
    "\n",
    "等号（=）用来给变量赋值。\n",
    "\n",
    "等号（=）运算符左边是一个变量名,等号（=）运算符右边是存储在变量中的值。例如："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "100\n",
      "1000.0\n",
      "runoob\n"
     ]
    }
   ],
   "source": [
    "counter = 100          # 整型变量\n",
    "miles   = 1000.0       # 浮点型变量\n",
    "name    = \"runoob\"     # 字符串\n",
    "\n",
    "print(counter)\n",
    "print(miles)\n",
    "print(name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 多个变量赋值\n",
    "Python允许你同时为多个变量赋值。例如："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "1\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "a = b = c = 1\n",
    "\n",
    "print(a)\n",
    "print(b)\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 解读\n",
    "\n",
    "以上实例，创建一个整型对象，值为 1，从后向前赋值，三个变量被赋予相同的数值。\n",
    "\n",
    "您也可以为多个对象指定多个变量。例如："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "runoob\n"
     ]
    }
   ],
   "source": [
    "a, b, c = 1, 2, \"runoob\"\n",
    "\n",
    "print(a)\n",
    "print(b)\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "以上实例，两个整型对象 1 和 2 的分配给变量 a 和 b，字符串对象 \"runoob\" 分配给变量 c。\n",
    "\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 标准数据类型\n",
    "\n",
    "Python3 中有六个标准的数据类型：\n",
    "\n",
    "+ Number（数字）\n",
    "+ String（字符串）\n",
    "+ List（列表）\n",
    "+ Tuple（元组）\n",
    "+ Set（集合）\n",
    "+ Dictionary（字典）\n",
    "\n",
    "Python3 的六个标准数据类型中：\n",
    "\n",
    "+ 不可变数据（3 个）：Number（数字）、String（字符串）、Tuple（元组）；\n",
    "+ 可变数据（3 个）：List（列表）、Dictionary（字典）、Set（集合）。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Number（数字）\n",
    "Python3 支持 int、float、bool、complex（复数）。\n",
    "\n",
    "在 Python3 里，只有一种整数类型 int，表示为长整型，没有 python2 中的 Long。\n",
    "\n",
    "像大多数语言一样，数值类型的赋值和计算都是很直观的。\n",
    "\n",
    "内置的 `type()` 函数可以用来查询变量所指的对象类型。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'int'> <class 'float'> <class 'bool'> <class 'complex'>\n"
     ]
    }
   ],
   "source": [
    "a, b, c, d = 20, 5.5, True, 4+3j\n",
    "print(type(a), type(b), type(c), type(d))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "此外还可以用 isinstance 来判断："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = 111\n",
    "isinstance(a, int)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "isinstance 和 type 的区别在于：\n",
    "\n",
    "+ type()不会认为子类是一种父类类型。\n",
    "+ isinstance()会认为子类是一种父类类型。\n",
    "\n",
    "这话有点绕，我们要先明白什么是类，然后什么是子类，什么是父类，我们一起来看看[《023-Python 面向对象》]()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "class A:\n",
    "    pass\n",
    "class B(A):\n",
    "    pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "isinstance(A(), A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(A()) == A "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "isinstance(B(), A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(B()) == A"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> 注意：在 Python2 中是没有布尔型的，它用数字 0 表示 False，用 1 表示 True。到 Python3 中，把 True 和 False 定义成关键字了，但它们的值还是 1 和 0，它们可以和数字相加。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "当你指定一个值时，Number 对象就会被创建："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "var1 = 1\n",
    "var2 = 10"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "您也可以使用del语句删除一些对象引用，这一块儿涉及到下面的好几个知识，我们可以最后回来看，但是你要知道有这么个 del 语句，还是很有用的。\n",
    "\n",
    "del语句的语法是：\n",
    "\n",
    "```\n",
    "del 变量名\n",
    "```\n",
    "\n",
    "实例，使用 del 方法删除一个普通变量："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "var1\n",
    "var2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'var1' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-94-c0dd3ea40a10>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;32mdel\u001b[0m \u001b[0mvar1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mvar1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m: name 'var1' is not defined"
     ]
    }
   ],
   "source": [
    "del var1\n",
    "var1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "实例，使用 del 语句删除列表中的元素："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4]"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list = [1,2,3,4]\n",
    "list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3]"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "del list[3] # 删除索引为 3 的数，索引从0开始算\n",
    "list"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "实例，使用 del 方法删除字典中的健值对："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': 'dfk', 'age': 18}"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "del dict\n",
    "d = dict(name='dfk',age=18)\n",
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': 'dfk'}"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "del d['age']\n",
    "d"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "上面的几个例子看起来很简单，但在某些情况瞎，理解起来有些困难。例如，下面的例子中，x和y都指向同一个列表："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['hello', 'world']"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = ['hello','world']\n",
    "y = x\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['hello', 'world']"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "有人会认为，使用 del 删除 x 后，y 也就随之消失了，但并非如此："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['hello', 'world']"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "del x\n",
    "y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "为什么会这样？x 和 y 都指向同一个列表。但是使用 del 删除 x 并不会影响 y。原因就是删除的只是名词，而不是列表本身（值）。事实上，在 Python 中是没有办法删除值的（也不需要过多考虑删除值的问题，因为在某个值不再使用的时候，Python 解释器会负责内存的回收）。\n",
    "\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 数值运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "5 + 4  # 加法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.3"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "4.3 - 2 # 减法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "21"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "3 * 7  # 乘法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.5"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2 / 4  # 除法，得到一个浮点数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2 // 4 # 除法，得到一个整数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "17 % 3 # 取余 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "32"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2 ** 5 # 乘方"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**注意：**\n",
    "\n",
    "1. Python可以同时为多个变量赋值，如a, b = 1, 2。\n",
    "2. 一个变量可以通过赋值指向不同类型的对象。\n",
    "3. 数值的除法包含两个运算符：/ 返回一个浮点数，// 返回一个整数。\n",
    "4. 在混合计算时，Python会把整型转换成为浮点数。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 数值类型实例\n",
    "\n",
    "|int   |\t float\t  |  complex |\n",
    "|---   |--------------|----------|\n",
    "|10\t   |           0.0|\t    3.14j|\n",
    "|100   |\t     15.20|\t     45.j|\n",
    "|-786  |\t     -21.9|9.322e-36j|\n",
    "|080   |\t  32.3e+18|\t    .876j|\n",
    "|-0490 |\t      -90.|\t-.6545+0J|\n",
    "|-0x260|\t-32.54e100|    3e+26J|\n",
    "|0x69  |\t  70.2E-12|\t 4.53e-7j|\n",
    "\n",
    "Python 还支持复数，复数由实数部分和虚数部分构成，可以用a + bj,或者complex(a,b)表示， 复数的实部a和虚部b都是浮点型"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## String（字符串）\n",
    "\n",
    "Python 中的字符串用单引号`'` 或双引号` \" `括起来，同时使用反斜杠` \\ `转义特殊字符。\n",
    "\n",
    "字符串的截取的语法格式如下：\n",
    "\n",
    "```\n",
    "变量[头下标:尾下标]\n",
    "```\n",
    "\n",
    "索引值以 0 为开始值，-1 为从末尾的开始位置。\n",
    "\n",
    "![字符串-索引-截取](https://pptwinpics.oss-cn-beijing.aliyuncs.com/%E5%AD%97%E7%AC%A6%E4%B8%B2-%E7%B4%A2%E5%BC%95-%E6%88%AA%E5%8F%96_20191121154452.png)\n",
    "\n",
    "加号`+`是字符串的连接符， 星号`*`表示复制当前字符串，紧跟的数字为复制的次数。实例如下：\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Runoob\n",
      "Runoo\n",
      "R\n",
      "noo\n",
      "noob\n",
      "RunoobRunoob\n",
      "RunoobTEST\n"
     ]
    }
   ],
   "source": [
    "str = 'Runoob'\n",
    " \n",
    "print (str)          # 输出字符串\n",
    "print (str[0:-1])    # 输出第一个到倒数第二个的所有字符\n",
    "print (str[0])       # 输出字符串第一个字符\n",
    "print (str[2:5])     # 输出从第三个开始到第五个的字符\n",
    "print (str[2:])      # 输出从第三个开始的后的所有字符\n",
    "print (str * 2)      # 输出字符串两次\n",
    "print (str + \"TEST\") # 连接字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Python 使用反斜杠`\\`转义特殊字符，如果你不想让反斜杠发生转义，可以在字符串前面添加一个`r`，表示原始字符串：\n",
    "\n",
    "在Python的`string`前面加上`'r'`， 是为了告诉编译器这个`string`是个`raw string`，不要转义`backslash '\\'` 。 例如，`\\n` 在raw string中，是两个字符，`\\`和`n`， 而不会转意为换行符。由于正则表达式和 `\\` 会有冲突，因此，当一个字符串使用了正则表达式后，最好在前面加上`'r'`。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Ru\n",
      "oob\n",
      "Ru\\noob\n"
     ]
    }
   ],
   "source": [
    "print('Ru\\noob')\n",
    "print(r'Ru\\noob')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "另外，反斜杠`\\`可以作为续行符，表示下一行是上一行的延续。也可以使用 \"\"\"...\"\"\" 或者 '''...''' 跨越多行。\n",
    "\n",
    "注意，Python 没有单独的字符类型，一个字符就是长度为1的字符串。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Le vent se lève, il faut tenter de vivre. \n",
      "起风了，唯有努力生存。\n",
      "（纵有疾风起，人生不言弃。）\n"
     ]
    }
   ],
   "source": [
    "str1 = '''Le vent se lève, il faut tenter de vivre. \n",
    "起风了，\\\n",
    "唯有努力生存。\n",
    "（纵有疾风起，人生不言弃。）'''\n",
    "\n",
    "print(str1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "P n\n",
      "n P\n"
     ]
    }
   ],
   "source": [
    "word = 'Python'\n",
    "\n",
    "print(word[0], word[5])\n",
    "print(word[-1], word[-6])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "与 C 字符串不同的是，Python 字符串不能被改变。向一个索引位置赋值，比如`word[0] = 'm'`会导致错误。\n",
    "\n",
    "注意：\n",
    "\n",
    "1. 反斜杠可以用来转义，使用r可以让反斜杠不发生转义。\n",
    "2. 字符串可以用`+`运算符连接在一起，用`*`运算符重复。\n",
    "3. Python中的字符串有两种索引方式，从左往右以0开始，从右往左以-1开始。\n",
    "4. Python中的字符串不能改变。\n",
    "\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# List（列表）\n",
    "\n",
    "List（列表） 是 Python 中使用最频繁的数据类型。\n",
    "\n",
    "列表可以完成大多数集合类的数据结构实现。列表中元素的类型可以不相同，它支持数字，字符串甚至可以包含列表（所谓嵌套）。\n",
    "\n",
    "列表是写在方括号` [] `之间、用逗号分隔开的元素列表。\n",
    "\n",
    "和字符串一样，列表同样可以被索引和截取，列表被截取后返回一个包含所需元素的新列表。\n",
    "\n",
    "列表截取的语法格式如下：\n",
    "\n",
    "```\n",
    "变量[头下标:尾下标]\n",
    "```\n",
    "\n",
    "索引值以 0 为开始值，-1 为从末尾的开始位置。\n",
    "\n",
    "![列表-截取](https://pptwinpics.oss-cn-beijing.aliyuncs.com/%E5%88%97%E8%A1%A8-%E6%88%AA%E5%8F%96_20191126114822.png)\n",
    "\n",
    "加号`+`是列表连接运算符，星号`*`是重复操作。如下实例："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['abcd', 786, 2.23, 'runoob', 70.2]\n",
      "abcd\n",
      "[786, 2.23]\n",
      "[2.23, 'runoob', 70.2]\n",
      "[123, 'runoob', 123, 'runoob']\n",
      "['abcd', 786, 2.23, 'runoob', 70.2, 123, 'runoob']\n"
     ]
    }
   ],
   "source": [
    "list = [ 'abcd', 786 , 2.23, 'runoob', 70.2 ]\n",
    "tinylist = [123, 'runoob']\n",
    " \n",
    "print (list)            # 输出完整列表\n",
    "print (list[0])         # 输出列表第一个元素\n",
    "print (list[1:3])       # 从第二个开始输出到第三个元素\n",
    "print (list[2:])        # 输出从第三个元素开始的所有元素\n",
    "print (tinylist * 2)    # 输出两次列表\n",
    "print (list + tinylist) # 连接列表"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "与Python字符串不一样的是，列表中的元素是可以改变的："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[9, 2, 13, 14, 15, 6]"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = [1, 2, 3, 4, 5, 6]\n",
    "a[0] = 9\n",
    "a[2:5] = [13, 14, 15]\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[9, 2, 6]"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[2:5] = []   # 将对应的元素值设置为 [] \n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "List 内置了有很多方法，例如 append()、pop() 等等，这在后面会讲到。\n",
    "\n",
    "**注意：**\n",
    "\n",
    "1. List写在方括号之间，元素用逗号隔开。\n",
    "2. 和字符串一样，list可以被索引和切片。\n",
    "3. List可以使用+操作符进行拼接。\n",
    "4. List中的元素是可以改变的。\n",
    "Python 列表截取可以接收第三个参数，参数作用是截取的步长，以下实例在索引 1 到索引 4 的位置并设置为步长为 2（间隔一个位置）来截取字符串：\n",
    "\n",
    "![列表-截取-2](https://pptwinpics.oss-cn-beijing.aliyuncs.com/%E5%88%97%E8%A1%A8-%E6%88%AA%E5%8F%96-2_20191126114918.png)\n",
    "\n",
    "如果第三个参数为负数表示逆向读取，以下实例用于翻转字符串："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "runoob like I\n"
     ]
    }
   ],
   "source": [
    "def reverseWords(input):\n",
    "     \n",
    "    # 通过空格将字符串分隔符，把各个单词分隔为列表\n",
    "    inputWords = input.split(\" \")\n",
    " \n",
    "    # 翻转字符串\n",
    "    # 假设列表 list = [1,2,3,4],  \n",
    "    # list[0]=1, list[1]=2 ，而 -1 表示最后一个元素 list[-1]=4 ( 与 list[3]=4 一样)\n",
    "    # inputWords[-1::-1] 有三个参数\n",
    "    # 第一个参数 -1 表示最后一个元素\n",
    "    # 第二个参数为空，表示移动到列表末尾\n",
    "    # 第三个参数为步长，-1 表示逆向\n",
    "    inputWords=inputWords[-1::-1]\n",
    " \n",
    "    # 重新组合字符串\n",
    "    output = ' '.join(inputWords)\n",
    "     \n",
    "    return output\n",
    " \n",
    "if __name__ == \"__main__\":\n",
    "    input = 'I like runoob'\n",
    "    rw = reverseWords(input)\n",
    "    print(rw)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Tuple（元组）\n",
    "元组（tuple）与列表类似，不同之处在于元组的元素不能修改。元组写在小括号 `()` 里，元素之间用逗号隔开。\n",
    "\n",
    "元组中的元素类型也可以不相同："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('abcd', 786, 2.23, 'runoob', 70.2)\n",
      "abcd\n",
      "(786, 2.23)\n",
      "(2.23, 'runoob', 70.2)\n",
      "(123, 'runoob', 123, 'runoob')\n",
      "('abcd', 786, 2.23, 'runoob', 70.2, 123, 'runoob')\n"
     ]
    }
   ],
   "source": [
    "tuple = ( 'abcd', 786 , 2.23, 'runoob', 70.2  )\n",
    "tinytuple = (123, 'runoob')\n",
    " \n",
    "print (tuple)             # 输出完整元组\n",
    "print (tuple[0])          # 输出元组的第一个元素\n",
    "print (tuple[1:3])        # 输出从第二个元素开始到第三个元素\n",
    "print (tuple[2:])         # 输出从第三个元素开始的所有元素\n",
    "print (tinytuple * 2)     # 输出两次元组\n",
    "print (tuple + tinytuple) # 连接元组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "元组与字符串类似，可以被索引且下标索引从0开始，-1 为从末尾开始的位置。也可以进行截取（看上面，这里不再赘述）。\n",
    "\n",
    "其实，可以把字符串看作一种特殊的元组。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    }
   ],
   "source": [
    "tup = (1, 2, 3, 4, 5, 6)\n",
    "print(tup[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(2, 3, 4, 5)\n"
     ]
    }
   ],
   "source": [
    "print(tup[1:5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'tuple' object does not support item assignment",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-49-a9915ee5ecf3>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtup\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m11\u001b[0m  \u001b[0;31m# 修改元组元素的操作是非法的\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
     ]
    }
   ],
   "source": [
    "tup[0] = 11  # 修改元组元素的操作是非法的"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "虽然tuple的元素不可改变，但它可以包含可变的对象，比如list列表。\n",
    "\n",
    "构造包含 0 个或 1 个元素的元组比较特殊，所以有一些额外的语法规则："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [],
   "source": [
    "tup1 = ()    # 空元组\n",
    "tup2 = (20,) # 一个元素，需要在元素后添加逗号"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "string、list 和 tuple 都属于 sequence（序列）。\n",
    "\n",
    "**注意：**\n",
    "\n",
    "1. 与字符串一样，元组的元素不能修改。\n",
    "2. 元组也可以被索引和切片，方法一样。\n",
    "3. 注意构造包含 0 或 1 个元素的元组的特殊语法规则。\n",
    "4. 元组也可以使用+操作符进行拼接。\n",
    "\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Set（集合）\n",
    "\n",
    "集合（set）是由一个或数个形态各异的大小整体组成的，构成集合的事物或对象称作元素或是成员。\n",
    "\n",
    "基本功能是进行成员关系测试和删除重复元素。\n",
    "\n",
    "可以使用大括号 `{ } `或者 `set()` 函数创建集合，注意：创建一个空集合必须用 `set() `而不是 `{ }`，因为 `{ } `是用来创建一个空字典。\n",
    "\n",
    "创建格式：\n",
    "\n",
    "```\n",
    "parame = {value01,value02,...}\n",
    "或者\n",
    "set(value)\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'Tom', 'Mary', 'Rose', 'Jack', 'Jim'}\n",
      "Rose 在集合中\n",
      "{'r', 'a', 'c', 'd', 'b'}\n",
      "{'r', 'b', 'd'}\n",
      "{'r', 'a', 'c', 'z', 'd', 'm', 'b', 'l'}\n",
      "{'c', 'a'}\n",
      "{'m', 'r', 'z', 'b', 'd', 'l'}\n"
     ]
    }
   ],
   "source": [
    "student = {'Tom', 'Jim', 'Mary', 'Tom', 'Jack', 'Rose'}\n",
    " \n",
    "print(student)   # 输出集合，重复的元素被自动去掉\n",
    " \n",
    "# 成员测试\n",
    "if 'Rose' in student :\n",
    "    print('Rose 在集合中')\n",
    "else :\n",
    "    print('Rose 不在集合中')\n",
    " \n",
    " \n",
    "# set可以进行集合运算\n",
    "a = set('abracadabra')\n",
    "b = set('alacazam')\n",
    " \n",
    "print(a)\n",
    " \n",
    "print(a - b)     # a 和 b 的差集\n",
    " \n",
    "print(a | b)     # a 和 b 的并集\n",
    " \n",
    "print(a & b)     # a 和 b 的交集\n",
    " \n",
    "print(a ^ b)     # a 和 b 中不同时存在的元素"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Dictionary（字典）\n",
    "字典（dictionary）是Python中另一个非常有用的内置数据类型。\n",
    "\n",
    "列表是有序的对象集合，字典是无序的对象集合。两者之间的区别在于：字典当中的元素是通过键来存取的，而不是通过偏移存取。\n",
    "\n",
    "字典是一种映射类型，字典用` { }` 标识，它是一个无序的**键(key) : 值(value)**的集合。\n",
    "\n",
    "键(key)必须使用不可变类型。\n",
    "\n",
    "在同一个字典中，键(key)必须是唯一的。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 - 菜鸟教程\n",
      "2 - 菜鸟工具\n",
      "{'name': 'runoob', 'code': 1, 'site': 'www.runoob.com'}\n",
      "dict_keys(['name', 'code', 'site'])\n",
      "dict_values(['runoob', 1, 'www.runoob.com'])\n"
     ]
    }
   ],
   "source": [
    "dict = {}\n",
    "dict['one'] = \"1 - 菜鸟教程\"\n",
    "dict[2]     = \"2 - 菜鸟工具\"\n",
    " \n",
    "tinydict = {'name': 'runoob','code':1, 'site': 'www.runoob.com'}\n",
    "\n",
    "print(dict['one'])       # 输出键为 'one' 的值\n",
    "print(dict[2])           # 输出键为 2 的值\n",
    "print(tinydict)          # 输出完整的字典\n",
    "print(tinydict.keys())   # 输出所有键\n",
    "print(tinydict.values()) # 输出所有值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "构造函数 dict() 可以直接从键值对序列中构建字典如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Runoob': 1, 'Google': 2, 'Taobao': 3}"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "del dict # 不加这一行，上面定义过的 dict 会对下面造成影响而报错\n",
    "dict([('Runoob', 1), ('Google', 2), ('Taobao', 3)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{2: 4, 4: 16, 6: 36}"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "{x: x**2 for x in (2, 4, 6)}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Runoob': 1, 'Google': 2, 'Taobao': 3}"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict(Runoob=1, Google=2, Taobao=3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "另外，字典类型也有一些内置的函数，例如clear()、keys()、values()等。\n",
    "\n",
    "**注意：**\n",
    "\n",
    "1. 字典是一种映射类型，它的元素是键值对。\n",
    "2. 字典的关键字必须为不可变类型，且不能重复。\n",
    "3. 字典使用 `{}`。\n",
    "\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python数据类型转换\n",
    "有时候，我们需要对数据内置的类型进行转换，数据类型的转换，你只需要将数据类型作为函数名即可。\n",
    "\n",
    "以下几个内置的函数可以执行数据类型之间的转换。这些函数返回一个新的对象，表示转换的值。\n",
    "\n",
    "|函数|描述|\n",
    "|---|---|\n",
    "|[int(x[,base])](https://github.com/DavidFnck/Python-Tutorial-for-Humans/blob/editing/004-Python%20%E5%9F%BA%E7%A1%80/int%20%E5%87%BD%E6%95%B0.ipynb)|将x转换为一个整数|\n",
    "|[float(x)]()|将x转换到一个浮点数|\n",
    "|complex(real [,imag])|创建一个复数|\n",
    "|str(x)|将对象x转换成字符串|"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
